package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.entity.BasicCustomLogoEntity;
import com.iwomy.secureplat.platform.service.BasicCustomLogoService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * LOGO设置表
 *
 * @author pig
 * @date 2023-11-06 15:41:52
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicCustomLogo" )
@Tag(description = "basicCustomLogo" , name = "LOGO设置表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicCustomLogoController {

    private final  BasicCustomLogoService basicCustomLogoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicCustomLogo LOGO设置表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicCustomLogo_view')" )
    public R getBasicCustomLogoPage(@ParameterObject Page page, @ParameterObject BasicCustomLogoEntity basicCustomLogo) {
		LambdaQueryWrapper<BasicCustomLogoEntity> wrapper = Wrappers.lambdaQuery(basicCustomLogo);
        return R.ok(basicCustomLogoService.page(page, wrapper));
    }


    /**
     * 通过id查询LOGO设置表
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("" )
    @PreAuthorize("@pms.hasPermission('platform_basicCustomLogo_view')" )
	public R getById() {
		return R.ok(basicCustomLogoService.getOne(Wrappers.query()));
	}

    /**
     * 新增LOGO设置表
     * @param basicCustomLogo LOGO设置表
     * @return R
     */
    @Operation(summary = "新增LOGO设置表" , description = "新增LOGO设置表" )
    @SysLog("新增LOGO设置表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicCustomLogo_add')" )
    public R save(@RequestBody BasicCustomLogoEntity basicCustomLogo) {
		BasicCustomLogoEntity basicCustomLogoEntity = basicCustomLogoService.getOne(Wrappers.query());
		if(ObjectUtil.isNotNull(basicCustomLogoEntity))
			basicCustomLogoService.removeById(basicCustomLogoEntity);
		basicCustomLogo.setDeptId(SecurityUtils.getUser().getDeptId());
        return R.ok(basicCustomLogoService.save(basicCustomLogo));
    }

    /**
     * 修改LOGO设置表
     * @param basicCustomLogo LOGO设置表
     * @return R
     */
    @Operation(summary = "修改LOGO设置表" , description = "修改LOGO设置表" )
    @SysLog("修改LOGO设置表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicCustomLogo_edit')" )
    public R updateById(@RequestBody BasicCustomLogoEntity basicCustomLogo) {
        return R.ok(basicCustomLogoService.updateById(basicCustomLogo));
    }

    /**
     * 通过id删除LOGO设置表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除LOGO设置表" , description = "通过id删除LOGO设置表" )
    @SysLog("通过id删除LOGO设置表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicCustomLogo_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicCustomLogoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicCustomLogo 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicCustomLogo_export')" )
    public List<BasicCustomLogoEntity> export(BasicCustomLogoEntity basicCustomLogo,Long[] ids) {
        return basicCustomLogoService.list(Wrappers.lambdaQuery(basicCustomLogo).in(ArrayUtil.isNotEmpty(ids), BasicCustomLogoEntity::getId, ids));
    }
}